﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using mamnontuoitho7.AppCode;
using System.Reflection;
using System.Data;

namespace mamnontuoitho7
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            try
            {
                //Load number of accessed user from xml file
                DataSet tmpDs = new DataSet();
                tmpDs.ReadXml(Server.MapPath("~/xml/counter.xml"));
                Application["Hits"] = tmpDs.Tables[0].Rows[0]["hits"].ToString();

            }
            catch (Exception ex)
            {
                Application["OnlineUsers"] = 0;
                Application["Hits"] = 0;
                Console.WriteLine(ex.Message);
            }
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            Session["username"] = "";
            Session["role"] = "";
            Session["memberID"] = "";
            Session["avatar"] = "";
            //Session["fullname"] = "";
            //Session["classID"] = "";

            //Common Session
            Session["Common_currentPageLink"] = "";
            Session["Common_articleTypeName"] = "";
            Session["Common_articleTypeID"] = "1";

            //Increase number of online user and accessed user.
            Application.Lock();
            Application["OnlineUsers"] = Convert.ToInt32(Application["OnlineUsers"]) + 1;
            Application["Hits"] = Convert.ToInt32(Application["Hits"]) + 1;

            try
            {
                //Save Number of accessed website to xml file
                DataSet tmpDs = new DataSet();
                tmpDs.ReadXml(Server.MapPath("~/xml/counter.xml"));
                tmpDs.Tables[0].Rows[0]["hits"] = Application["Hits"].ToString();
                tmpDs.WriteXml(Server.MapPath("~/xml/counter.xml"));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Application.UnLock();

            if (Request.Cookies["CurrentBar"] == null)
            {
                Response.Cookies["CurrentBar"].Value = "1";
                Response.Cookies["CurrentBar"].Expires = DateTime.Now.AddDays(1);
            }

            //***********dùng để disable chức năng Files Change Monitor cùa asp.Net, **********************\\
            //***********chức năng Files Change Monitor này sẽ expire tất cả biến Session *****************\\
            //***********khi có sự thay đỗi về file hay folder ex: xóa folder******************************\\
            PropertyInfo p = typeof(System.Web.HttpRuntime).GetProperty("FileChangesMonitor", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
            object o = p.GetValue(null, null);
            FieldInfo f = o.GetType().GetField("_dirMonSubdirs", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
            object monitor = f.GetValue(o);
            MethodInfo m = monitor.GetType().GetMethod("StopMonitoring", BindingFlags.Instance | BindingFlags.NonPublic);
            m.Invoke(monitor, new object[] { });
            //*********************************************************************************************\\
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {
            //Decrease number of online user
            Application.Lock();
            Application["OnlineUsers"] = Convert.ToInt32(Application["OnlineUsers"]) - 1;
            Application.UnLock();
        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}